﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TinyCinema;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Timers;
using TinyCinema.Bussiness;
using TinyCinema.Data;


namespace TinyCinema.Bussiness.Schedule
{
    public class BookedPayment : DependencyObject
    {
        public static DependencyProperty BookedTicketProperty = DependencyProperty.Register("BookedTicket", typeof(Data.BookedTicket), typeof(BookedPayment));
        public Data.BookedTicket BookedTicket
        {
            get { return (Data.BookedTicket)GetValue(BookedTicketProperty); }
            set
            {
                SetValue(BookedTicketProperty, value);
            }
        }
        public static DependencyProperty IsBookedTickedFoundProperty = DependencyProperty.Register("IsBookedTickedFound", typeof(bool), typeof(BookedPayment));
        public bool IsBookedTickedFound
        {
            get { return (bool)GetValue(IsBookedTickedFoundProperty); }
            set
            {
                SetValue(IsBookedTickedFoundProperty, value);
            }
        }
        public ICommand CheckCommand { get; set; }
        public ICommand ClearCommand { get; set; }
        public ICommand PayCommand { get; set; }
        public ICommand DeleteCommand { get; set; }
        public Messenger Messenger { get; set; }
        Timer BookedTickedCheckTimer = new Timer();
        public BookedPayment()
        {
            Messenger = new Bussiness.Messenger();
            IsBookedTickedFound = false;
            BookedTicket = new Data.BookedTicket();            
            BookedTickedCheckTimer.Interval = 5000;
            BookedTickedCheckTimer.Elapsed += BookedTickedCheckTimer_Elapsed;            
            CheckCommand = new RelayCommand<string>(
                (s) => true,
                (s) =>
                {
                    try
                    {
                        BookedTicket = new Data.BookedTicket(s);
                    }
                    catch
                    {
                        Messenger.Send(new Message.BookedTicketNotFound());
                        IsBookedTickedFound = false;
                        return;
                    }
                    if (BookedTicket.IsCanceled)
                    {
                        IsBookedTickedFound = false;
                        Messenger.Send(new Message.BookedTicketCanceled());
                    }
                    else
                        if (BookedTicket.IsPurchased)
                        {
                            IsBookedTickedFound = false;
                            Messenger.Send(new Message.BookedTicketPurchased());
                        }
                        else IsBookedTickedFound = true;
                });
            ClearCommand = new RelayCommand<string>(
                (s) => true,
                (s) =>
                {
                    IsBookedTickedFound = false;
                });
            PayCommand = new RelayCommand<Data.Employee>(
                (s) => true,
                (s) =>
                {
                    var message = new Message.PayConfirmation();
                    Messenger.Send(message);
                    if (message.Result == MessageBoxResult.Yes)
                    {
                        new Data.TicketOrder(BookedTicket, s);
                        IsBookedTickedFound = false;
                        Messenger.Send(new Message.PayConfirmationOK());
                    }
                });

            DeleteCommand = new RelayCommand<string>(
                (s) => true,
                (s) =>
                {
                    var message = new Message.DeleteConfirmation();
                    Messenger.Send(message);
                    if (message.Result==MessageBoxResult.Yes)
                    {
                        BookedTicket.Delete();
                        IsBookedTickedFound = false;
                        Messenger.Send(new Message.Deleted());
                    }
                });
            
        }

        public void StartBookedTickedCheck()
        {
            BookedTickedCheckTimer.Enabled = true;
            BookedTickedCheckTimer.Start();
        }
        public void StopBookedTickedCheck()
        {
            BookedTickedCheckTimer.Enabled = false;
            BookedTickedCheckTimer.Stop();
        }

        void BookedTickedCheckTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Execution.PutNewTableIntoDataSet(Tables.Ticket);
            var tbl = Execution.ds.Tables[Tables.Ticket];
            if (tbl == null) return;
            var Rows = tbl.Select(TicketTable.TicketOrder + " IS NULL");
            foreach(var Row in Rows)
            {
                Data.Schedule schedule = new Data.Schedule(Row[TicketTable.ScheduleID] as string);
                var Con1 = schedule.Day.Year == DateTime.Now.Year;
                var Con2 = schedule.Day.Month == DateTime.Now.Month;
                var Con3 = schedule.Day.Day == DateTime.Now.Day;
                var Con4 = (schedule.Time-DateTime.Now.TimeOfDay).Minutes < 20;
                if(Con1&&Con2&&Con3&&Con4) Row.Delete();
            }
            Execution.SaveCommand(Tables.Ticket);
        }
    }
}
